'use strict';
app.controller('chainCtrl', ['$scope', '$log', 'utilService', '$modal', function ($scope, $log, utilService, $modal) {
    $scope.chain = {};
    // call server.
    $scope.query = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'chain',
            params: $scope.chain
        }).then(function (result) {
        });
    }
    $scope.query();

    //更新状态
    $scope.openUpdateStatus = function (id, status, name) {
        var modalInstance = $modal.open({
            templateUrl: 'updateUserStatus.html',
            controller: 'updateUserStatusCtrl',
            scope: $scope,
            resolve: {
                data: function () {
                    var data = {};
                    data.id = id;
                    data.status = status;
                    data.name = name;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };

    $scope.openChainAudit = function (id, name, auditStatus) {
        var param = {
            id: id,
            name: name,
            auditStatus: auditStatus
        };
        // query data.
        utilService.modal($modal, $scope, {
            title: "审核",
            controller: "chainAuditCtrl",
            message: "你确定【" + name + "】 连锁品牌审核通过吗？",
            data: param
        });
    }

    //打开查看服务框
    $scope.checkReserve = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/chian/reserve_info.html',
            controller: 'reserveInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

app.controller('reserveInfoCtrl',['$scope','toaster','data','$modalInstance','chainService',
    function($scope,toaster,data,$modalInstance,chainService){
    $scope.id=data;
    $scope.goodsInfo={};

    $scope.checkReserve=function(){
        chainService.checkReserve($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkReserve();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);

app.controller('updateMerchantsCtrl', ['$scope', 'utilService', 'chainService', 'toaster', function ($scope, utilService, chainService, toaster) {
    $scope.updateStatus = function (id, status) {
        chainService.updateSystemUserStatus($scope, id, status).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
}]);


app.controller('chainViewCtrl', ['$scope', 'chainService', '$stateParams', 'toaster', '$state', '$filter', '$window','utilService','$modal',
    function ($scope, chainService, $stateParams, toaster, $state, $filter, $window,utilService,$modal) {
        $scope.getIndustryFullName = function () {
            chainService.getIndustryFullName($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.industryData = result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }

            });
        }
        $scope.getIndustryFullName();
        $scope.getSetMealName = function () {
            chainService.getSetMealName($scope).then(
                function (result) {
                    switch (result.status) {
                        case 200:
                            $scope.setMealData = result.data;
                            break;
                        default :
                            toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                            break;
                    }
                }
            );
        }
        $scope.getSetMealName();

        $scope.chainId = $stateParams.chainId;

        $scope.findById = function () {
            chainService.findById($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.chain = result.data;
                        $scope.chain.expireTime=$filter('date')($scope.chain.expireTime,'yyyy-MM-dd');
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }

            });
        }

        $scope.findById();

        $scope.editChain = function () {
            $scope.chain.expireTime=$filter('date')($scope.chain.expireTime,'yyyy-MM-dd');
            chainService.updateChain($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        console.info(result);
                        $state.go("app.chain");
                        break;
                    case 604:
                        toaster.pop('error', "操作失败", "行业名称已存在");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }

        $scope.compare=function(){
            var now=new Date();
            if($scope.chain.expireTime!=null){
                var time=new Date($scope.chain.expireTime);
                if(time.getTime() < now.getTime()){
                    return true;
                }
            }
            return false;
        };

        $scope.selectSell = function(isOpenSell){
            if(isOpenSell == 0){
                var param = {
                    isOpenSell: isOpenSell
                };
                // query data.
                utilService.modal($modal, $scope, {
                    title: "警告",
                    controller: "updateSellCtrl",
                    message: "警告:如把平台分销关闭,系统会把平台下的商品及商家状态都改为【关闭分销】",
                    data: param
                });
            }
        }

        $scope.selectRebate = function(isOpenRebate){
            if(isOpenRebate == 0){
                var param = {
                    isOpenRebate: isOpenRebate
                };
                // query data.
                utilService.modal($modal, $scope, {
                    title: "警告",
                    controller: "updateSellCtrl",
                    message: "警告:如把平台排队返利关闭,系统会把平台下的商品及商家状态都改为【关闭返利】",
                    data: param
                });
            }
        }

    }]);

app.controller('updateSellCtrl', ['$scope', '$modalInstance','modalData',
    function ($scope, $modalInstance,modalData) {
        $scope.modal = modalData;
        $scope.systemConfig = {
            id: modalData.data.id,
            isOpenSell: modalData.data.isOpenRebate
        };
        $scope.ok = function () {
            $scope.cancel();
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

app.controller('updateUserStatusCtrl', ['$scope', '$modalInstance', '$state', 'merchantsService', 'toaster', 'data',
    function ($scope, $modalInstance, $state, merchantsService, toaster, data) {
        $scope.data = data;
        $scope.AllowToDelete = true;

        $scope.ok = function () {
            if (!$scope.AllowToDelete) {
                return;
            }
            $scope.AllowToDelete = false;

            merchantsService.updateSystemUserStatus($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }])

//店铺审核通过弹窗
app.controller('merchantsAuditCtrl', ['$scope', '$modalInstance', '$state', 'chainService', 'toaster', 'modalData',
    function ($scope, $modalInstance, $state, chainService, toaster, modalData) {
        $scope.modal = modalData;
        $scope.chain = {
            id: modalData.data.id,
            auditStatus: modalData.data.auditStatus
        };
        $scope.AllowToDelete = true;

        $scope.ok = function () {
            if (!$scope.AllowToDelete) {
                return;
            }
            $scope.AllowToDelete = false;
            chainService.updateChainShopAudit($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

//连锁品牌审核通过弹窗
app.controller('chainAuditCtrl', ['$scope', '$modalInstance', '$state', 'chainService', 'toaster', 'modalData',
    function ($scope, $modalInstance, $state, chainService, toaster, modalData) {
        $scope.modal = modalData;
        $scope.chain = {
            id: modalData.data.id,
            auditStatus: modalData.data.auditStatus
        };
        $scope.AllowToDelete = true;

        $scope.ok = function () {
            if (!$scope.AllowToDelete) {
                return;
            }
            $scope.AllowToDelete = false;
            chainService.updateChainAudit($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);


app.controller('infoCtrl', ['$scope', '$modalInstance', '$state', 'merchantsService', 'toaster', 'modalData',
    function ($scope, $modalInstance, $state, merchantsService, toaster, modalData) {
        $scope.modal = modalData;
        $scope.merchants = {
            id: modalData.data.id
        };

        $scope.ok = function () {
            $modalInstance.dismiss('success');
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

//审核不通过
app.controller('chainAuditFailCtrl', ['$scope', '$stateParams', 'chainService', 'toaster', '$state', function ($scope, $stateParams, chainService, toaster, $state) {
    $scope.chain = {
        id: $stateParams.id,
        auditStatus: 1
    };
    $scope.confirm = function () {
        console.info($scope)
        chainService.updateChainAudit($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go("app.chain");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

}]);

app.controller('chainShopCtrl', ['$scope', '$log', 'utilService', '$modal', '$stateParams', function ($scope, $log, utilService, $modal, $stateParams) {
    $scope.shop = {chainId: $stateParams.chainId};
    $scope.query = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'shop',
            params: $scope.shop
        }).then(function (result) {
        });
    }
    $scope.query();


    $scope.openMerchantsAudit = function (id, name, auditStatus) {
        var param = {
            id: id,
            name: name,
            auditStatus: auditStatus
        };
        // query data.
        utilService.modal($modal, $scope, {
            title: "审核",
            controller: "merchantsAuditCtrl",
            message: "你确定【" + name + "】 商户审核通过吗？",
            data: param
        });
    }

    //更新状态
    $scope.openUpdateStatus = function (id, status, name) {
        var modalInstance = $modal.open({
            templateUrl: 'updateUserStatus.html',
            controller: 'updateUserStatusCtrl',
            scope: $scope,
            resolve: {
                data: function () {
                    var data = {};
                    data.id = id;
                    data.status = status;
                    data.name = name;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };
}]);

app.controller('updateUserStatusCtrl', ['$scope', '$modalInstance', '$state', 'merchantsService', 'toaster', 'data',
    function ($scope, $modalInstance, $state, merchantsService, toaster, data) {
        $scope.data = data;
        $scope.AllowToDelete = true;

        $scope.ok = function () {
            if (!$scope.AllowToDelete) {
                return;
            }
            $scope.AllowToDelete = false;

            merchantsService.updateSystemUserStatus($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.query();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

//审核不通过
app.controller('shopAuditFailCtrl', ['$scope', '$stateParams', 'merchantsService', 'toaster', '$state', function ($scope, $stateParams, merchantsService, toaster, $state) {
    $scope.merchants = {
        id: $stateParams.id,
        auditStatus: 1
    };
    $scope.confirm = function () {
        merchantsService.updateMerchantsAudit($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go("app.merchants");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

}]);